package com.eee.machine.service.impl;

import com.eee.machine.common.TableData;
import com.eee.machine.service.ISalesManService;
import com.eee.machine.utils.youzan.TokenUtil;
import com.youzan.open.sdk.client.auth.Token;
import com.youzan.open.sdk.client.core.DefaultYZClient;
import com.youzan.open.sdk.client.core.YZClient;
import com.youzan.open.sdk.client.oauth.model.OAuthToken;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanSalesmanAccountGet;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanSalesmanAccountsGet;
import com.youzan.open.sdk.gen.v3_0_0.model.*;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ISalesManServiceImpl implements ISalesManService {

    @Autowired
    private TokenUtil tokenUtil;
    @Override
    public TableData getAllSalesMan(Long page, Long pageSize, String phone) {
        String accessToken = tokenUtil.getAccessToken();
        System.out.println("授权token====="+accessToken);
//        OAuthToken token = TokenUtil.getToken();
        YZClient client = new DefaultYZClient(new Token(accessToken)); //new Sign(appKey, appSecret)
//        OAuthToken token = TokenUtil.getToken();
//        YZClient client = new DefaultYZClient(new Token(token.getAccessToken())); //new Sign(appKey, appSecret)
        if (!Strings.isBlank(phone)) {
            return this.findSaleMan(phone);
        } else {
            YouzanSalesmanAccountsGetParams youzanSalesmanAccountsGetParams = new YouzanSalesmanAccountsGetParams();
            youzanSalesmanAccountsGetParams.setPageNo(page);
            youzanSalesmanAccountsGetParams.setPageSize(pageSize);
            YouzanSalesmanAccountsGet youzanSalesmanAccountsGet = new YouzanSalesmanAccountsGet();
            youzanSalesmanAccountsGet.setAPIParams(youzanSalesmanAccountsGetParams);
            YouzanSalesmanAccountsGetResult result = client.invoke(youzanSalesmanAccountsGet);
            TableData resultObject = new TableData("获取数据成功", 0, result.getTotalResults().intValue(), result.getAccounts());
            return resultObject;
        }
    }

    @Override
    public TableData findSaleMan(String phone) {
//        OAuthToken token = TokenUtil.getToken();
//        YZClient client = new DefaultYZClient(new Token(token.getAccessToken())); //new Sign(appKey, appSecret)
        String accessToken = tokenUtil.getAccessToken();
        System.out.println("授权token====="+accessToken);
//        OAuthToken token = TokenUtil.getToken();
        YZClient client = new DefaultYZClient(new Token(accessToken)); //new Sign(appKey, appSecret)
        YouzanSalesmanAccountGetParams youzanSalesmanAccountGetParams = new YouzanSalesmanAccountGetParams();
        youzanSalesmanAccountGetParams.setMobile(phone);
        youzanSalesmanAccountGetParams.setFansType(1L);
        youzanSalesmanAccountGetParams.setFansId(0L);
        YouzanSalesmanAccountGet youzanSalesmanAccountsGet = new YouzanSalesmanAccountGet();
        youzanSalesmanAccountsGet.setAPIParams(youzanSalesmanAccountGetParams);
        YouzanSalesmanAccountGetResult result = client.invoke(youzanSalesmanAccountsGet);
        List<YouzanSalesmanAccountGetResult> results = new ArrayList<>();
        results.add(result);
        TableData resultObject = new TableData("获取数据成功", 0, 1, results);
        return resultObject;
    }
}
